<template>
  <view class="register-container">
    <view class="header">
      <view class="welcome">
        <text class="title">注册账号</text>
        <text class="subtitle">工厂管理系统</text>
      </view>
    </view>
    
    <view class="form-box">
      <view class="input-group">
        <!-- 用户名 -->
        <view class="input-item">
          <text class="icon">👤</text>
          <input 
            type="text"
            v-model="formData.username"
            placeholder="请输入用户名"
            placeholder-class="placeholder"
          />
        </view>
        
        <!-- 密码 -->
        <view class="input-item">
          <text class="icon">🔒</text>
          <input 
            type="password"
            v-model="formData.password"
            placeholder="请输入密码"
            placeholder-class="placeholder"
          />
        </view>
        
        <!-- 确认密码 -->
        <view class="input-item">
          <text class="icon">🔒</text>
          <input 
            type="password"
            v-model="formData.confirmPassword"
            placeholder="请确认密码"
            placeholder-class="placeholder"
          />
        </view>
        
        <!-- 昵称 -->
        <view class="input-item">
          <text class="icon">📝</text>
          <input 
            type="text"
            v-model="formData.nickname"
            placeholder="请输入昵称"
            placeholder-class="placeholder"
          />
        </view>
        
        <!-- 手机号 -->
        <view class="input-item">
          <text class="icon">📱</text>
          <input 
            type="number"
            v-model="formData.phone"
            placeholder="请输入手机号"
            placeholder-class="placeholder"
            maxlength="11"
          />
        </view>
      </view>
      
      <button class="submit-btn" @tap="handleRegister" :loading="loading" :disabled="loading">
        <text>{{ loading ? '注册中...' : '注 册' }}</text>
      </button>
      
      <view class="actions">
        <text class="login" @tap="handleLogin">已有账号？立即登录</text>
      </view>
    </view>
    
    <view class="footer">
      <text class="copyright">© 2025 工厂管理系统</text>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import auth from '@/api/auth'

const loading = ref(false)
const formData = ref({
  username: '',
  password: '',
  confirmPassword: '',
  nickname: '',
  phone: ''
})

// 表单验证
function validateForm() {
  if (!formData.value.username) {
    uni.showToast({
      title: '请输入用户名',
      icon: 'none'
    })
    return false
  }
  if (!formData.value.password) {
    uni.showToast({
      title: '请输入密码',
      icon: 'none'
    })
    return false
  }
  if (formData.value.password !== formData.value.confirmPassword) {
    uni.showToast({
      title: '两次输入的密码不一致',
      icon: 'none'
    })
    return false
  }
  if (!formData.value.nickname) {
    uni.showToast({
      title: '请输入昵称',
      icon: 'none'
    })
    return false
  }
  if (!formData.value.phone) {
    uni.showToast({
      title: '请输入手机号',
      icon: 'none'
    })
    return false
  }
  if (!/^1[3-9]\d{9}$/.test(formData.value.phone)) {
    uni.showToast({
      title: '手机号格式不正确',
      icon: 'none'
    })
    return false
  }
  return true
}

// 处理注册
async function handleRegister() {
  if (!validateForm()) return
  
  try {
    loading.value = true
	console.log(formData.value.password)
    
    await auth.register({
      username: formData.value.username,
      password: formData.value.password,
      nickname: formData.value.nickname,
      phone: formData.value.phone
    })
    
    uni.showToast({
      title: '注册成功',
      icon: 'success'
    })
    
    // 跳转到登录页
    setTimeout(() => {
      uni.redirectTo({
        url: '/pages/login/index'
      })
    }, 1500)
  } catch (error) {
    uni.showToast({
      title: error.message || '注册失败',
      icon: 'none'
    })
  } finally {
    loading.value = false
  }
}

// 跳转到登录页
function handleLogin() {
  uni.redirectTo({
    url: '/pages/login/index'
  })
}
</script>

<style lang="scss">
.register-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #1296db, #6bc6ff);
  display: flex;
  flex-direction: column;
  
  .header {
    padding: 120rpx 60rpx;
    
    .welcome {
      .title {
        font-size: 48rpx;
        color: #fff;
        font-weight: bold;
        margin-bottom: 16rpx;
        display: block;
      }
      
      .subtitle {
        font-size: 32rpx;
        color: rgba(255, 255, 255, 0.8);
      }
    }
  }
  
  .form-box {
    flex: 1;
    background-color: #fff;
    border-radius: 40rpx 40rpx 0 0;
    padding: 60rpx 40rpx;
    box-shadow: 0 -4rpx 16rpx rgba(0, 0, 0, 0.1);
    
    .input-group {
      margin: 40rpx 0;
      
      .input-item {
        background-color: #f8f8f8;
        border-radius: 16rpx;
        height: 100rpx;
        display: flex;
        align-items: center;
        padding: 0 30rpx;
        margin-bottom: 30rpx;
        transition: all 0.3s;
        
        &:focus-within {
          background-color: #fff;
          box-shadow: 0 4rpx 16rpx rgba(18, 150, 219, 0.1);
        }
        
        .icon {
          font-size: 40rpx;
          margin-right: 20rpx;
        }
        
        input {
          flex: 1;
          font-size: 30rpx;
          height: 100%;
        }
      }
    }
    
    .placeholder {
      color: #bbb;
      font-size: 30rpx;
    }
    
    .submit-btn {
      background: linear-gradient(135deg, #1296db, #6bc6ff);
      color: #fff;
      height: 100rpx;
      line-height: 100rpx;
      border-radius: 50rpx;
      font-size: 32rpx;
      font-weight: bold;
      margin: 60rpx 0;
      box-shadow: 0 8rpx 16rpx rgba(18, 150, 219, 0.2);
      transition: all 0.3s;
      
      &:active {
        transform: translateY(2rpx);
        box-shadow: 0 4rpx 8rpx rgba(18, 150, 219, 0.2);
      }
      
      &[disabled] {
        opacity: 0.7;
        background: linear-gradient(135deg, #1296db, #6bc6ff);
      }
    }
    
    .actions {
      text-align: center;
      font-size: 28rpx;
      padding: 0 20rpx;
      
      .login {
        color: #1296db;
        font-weight: bold;
      }
    }
  }
  
  .footer {
    text-align: center;
    padding: 40rpx;
    background-color: #fff;
    
    .copyright {
      font-size: 24rpx;
      color: #999;
    }
  }
}
</style> 